
import { getAiFiles, addAiFile, deleteFiles,updateAiFile, postAiRecord, getAiRecord } from "@/api";
import { FilesType, GetRecordType } from "@/types/apiResType";

export type IFileMagage = {
  currentFileid: string,
  cuttentContent: string,
  currentTitle:string,
};

export enum EPageType {
  editor = 'editor',
  list = 'list'
}

export interface IFileMagageStore {
  pageType: EPageType,
  currentFile: IFileMagage,
  filesRes: FilesType,
  recordInfo:GetRecordType,
  currentRecord:Pick< GetRecordType,'record_id'>,
  reset:()=>void
  setcurrentfileId: (title: string) => void
  setTitle: (title: string) => void
  getFiles: (page: string) => Promise<void>
  getRecord: (fileid: string) => Promise<void>
  postRecord: (param: { record: string}) => Promise<void>
  addFile: (name: string) => Promise<void>
  deleteFile: (fileid:string,succFn:()=>void) => Promise<void>
  updateFile: (name: string) => Promise<void>
}

const FileMagageStore = (): IFileMagageStore => {
  return {
    /**当前文件状态 */
    currentFile: { currentFileid: '', cuttentContent: '' ,currentTitle:''},
    currentRecord: {
      record_id: ""
    },
    recordInfo:{
      record_id: "",
      record: {
        create: "",
        update: "",
        record: ""
      }
    },
    filesRes: {
      count: 0,
      list: []
    },
    /**默认编辑模式 */
    pageType: EPageType.editor,
    reset(){
      this.currentFile.currentFileid=''
      this.currentFile.currentTitle=''
      this.currentRecord.record_id=''
      this.recordInfo={record_id: "",
      record: {
        create: "",
        update: "",
        record: ""
      }}
    },
    setTitle(title:string){
      this.currentFile.currentTitle=title;
    },
    setcurrentfileId(id:string){
      this.currentFile.currentFileid=id;
    },
    async getRecord(fileid:string){
      const res=await getAiRecord({fileid});
      if(res.code){
        /**如果返回长度为0 */
        if(!res.data.record_id){ 
          await this.postRecord({record:''})
        }else{
          this.recordInfo=res.data;
          this.currentRecord.record_id=res.data.record_id
        }
      }
    },
    async postRecord(param: { record: string}){

      if(!this.currentFile.currentFileid){
        await this.addFile(this.currentFile.currentTitle)
      } 
      const res=await postAiRecord(this.currentFile.currentFileid,{...param,record_id:this.currentRecord.record_id})
      if(res.data){
        this.currentRecord.record_id=res.data.record_id
      }
    },
    async getFiles(page: string) {
      const res = await getAiFiles({
        page
      })
      if (res.code) {
        this.filesRes = res.data
      }
    },
    async addFile(name: string) {
      const res = await addAiFile({
        name: name
      })
      if (res.code) {
        this.currentFile.currentFileid = res.data.fileid
      }
    },
    async updateFile(name: string) {
      const res = await updateAiFile({
        fileid: this.currentFile.currentFileid,
        name: name
      })
    },
    async deleteFile(fileid:string,succFn:()=>void){
      const res=await deleteFiles({
        fileid: fileid
      })
      if(res.code){
        succFn()
      }
    }

  };
};

export default FileMagageStore;
